package com.lean.android.ad.core.feed

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RestrictTo
import com.bytedance.sdk.openadsdk.TTAdDislike
import com.bytedance.sdk.openadsdk.mediation.ad.MediationExpressRenderListener
import com.lean.android.ad.core.AdRenderCallback
import com.lean.android.ad.core.AdRenderer
import com.lean.android.ad.repository.ReportHelper
import com.lean.android.ad.repository.ReportHelper.toCpmInt
import com.lean.android.ad.utils.AdLog
import com.lean.android.ad.utils.DisplayHelper

@RestrictTo(RestrictTo.Scope.LIBRARY)
class FeedAdRenderer(
    private val activity: Activity, private val adContainer: ViewGroup
) : AdRenderer<FeedAd, AdRenderCallback> {

    companion object {
        private val reportedRequestIdSet = hashSetOf<String>()
    }

    override fun render(ad: FeedAd, renderCallback: AdRenderCallback) {
        val nativeAd = ad.nativeAd
        if (nativeAd.mediationManager.isExpress) {
            nativeAd.setDislikeCallback(activity, object : TTAdDislike.DislikeInteractionCallback {
                override fun onShow() {// Dislike show

                }

                override fun onSelected(position: Int, value: String?, enforce: Boolean) {
                    AdLog.d(AdLog.AdFeedLoadTag, "FeedAd on dislike clicked ")
                    adContainer.removeAllViews()
                    reportedRequestIdSet.remove(ad.requestId)
                    ad.destroy()
                    renderCallback.onAdClosed(ad)
                }

                override fun onCancel() {

                }
            })

            nativeAd.setExpressRenderListener(object : MediationExpressRenderListener {
                override fun onRenderSuccess(v: View?, w: Float, h: Float, p3: Boolean) {
                    val adView = ad.adView
                    if (adView != null) {
                        val adLayoutParams = ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
                        )
                        DisplayHelper.removeFromParent(adView)
                        adContainer.removeAllViews()

                        adContainer.addView(adView, adLayoutParams)
                        AdLog.d(AdLog.AdFeedLoadTag, "FeedAd onRenderSuccess. ")
                    } else {
                        AdLog.e(AdLog.AdFeedLoadTag, "FeedAd onRenderSuccess, but adView is null")
                    }
                }

                override fun onRenderFail(v: View?, p1: String?, p2: Int) {
                    AdLog.e(AdLog.AdFeedLoadTag, "FeedAd onRenderFail code $p2, message $p1`")
                    renderCallback.onAdShowFail(p2, p1)
                }

                override fun onAdClick() {
                    renderCallback.onAdClicked(ad)
                }

                override fun onAdShow() {
                    val reported = reportedRequestIdSet.contains(ad.requestId)
                    if (!reported) {
                        ad.nativeAd.mediationManager?.showEcpm?.also { cpmInfo ->
                            val cpm = cpmInfo.ecpm.toCpmInt()

                            ReportHelper.reportAdEvent(
                                requestId = ad.requestId,
                                eventType = ReportHelper.AdEventType.AdShowed,
                                platformId = cpmInfo.sdkName.orEmpty(),
                                adPosition = ReportHelper.AdPositionType.Feed,
                                cpm = cpm
                            )

                            reportedRequestIdSet.add(ad.requestId)
                        }
                    }

                    adContainer.post {
                        renderCallback.onAdShow(ad, adContainer.width, adContainer.height)
                    }
                }
            })

            nativeAd.render()
        } else {
            renderCallback.onAdShowFail(-1, "自渲染暂不支持")
        }
    }
}